package com.sunmouse.toollib.utils;

import android.content.Context;
import android.content.res.Resources;
import android.support.annotation.StringRes;
import android.text.TextUtils;
import android.view.Gravity;
import android.widget.Toast;

import com.sunmouse.toollib.R;

/**
 * Created by SC-002 on 2016/12/16.
 */
public class ToastUtils {
    public static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
    public static final int DEFAULT_X_OFFSET = 0;
    private static int yOffset;

    /**
     * simple toast
     */
    public static void simpleToast(Context context, String msg, @StringRes int res, int gravity, int xOffset, int yOffset) {
        Toast mToast = null;
        if (!TextUtils.isEmpty(msg)) {
            mToast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
        } else if (res > 0) {
            mToast = Toast.makeText(context, res, Toast.LENGTH_SHORT);
        }
        if (mToast == null) return;
        mToast.setGravity(gravity < 0 ? DEFAULT_GRAVITY : gravity, xOffset, yOffset);
        mToast.show();
    }

    public static void simpleToast(Context context, String msg, int gravity) {
        if (TextUtils.isEmpty(msg)) {
            throw new NullPointerException("msg is null");
        }
        yOffset = context.getResources().getDimensionPixelSize(R.dimen.toast_default_y_offset);
        simpleToast(context, msg, -1, gravity, DEFAULT_X_OFFSET, yOffset);
    }

    public static void simpleToast(Context context, String msg, int gravity, int yOffset) {
        if (TextUtils.isEmpty(msg)) {
            throw new NullPointerException("msg is null");
        }
        simpleToast(context, msg, -1, gravity, DEFAULT_X_OFFSET, yOffset);
    }

    public static void simpleToast(Context context, String msg) {
        if (TextUtils.isEmpty(msg)) {
            throw new NullPointerException("msg is null");
        }
        yOffset = context.getResources().getDimensionPixelSize(R.dimen.toast_default_y_offset);
        simpleToast(context, msg, -1, DEFAULT_GRAVITY, DEFAULT_X_OFFSET, yOffset);
    }

    public static void simpleToast(Context context, @StringRes int res) {
        if (res <= 0) {
            throw new Resources.NotFoundException("res cannot be found !");
        }
        yOffset = context.getResources().getDimensionPixelSize(R.dimen.toast_default_y_offset);
        simpleToast(context, "", res, DEFAULT_GRAVITY, DEFAULT_X_OFFSET, yOffset);
    }


    /**
     * when needing to toast much
     */
    private static Context mContext;
    private static ToastUtils mToastUtils;

    public static ToastUtils init(Context context) {
        mContext = context;
        if (mToastUtils == null)
            mToastUtils = new ToastUtils();
        return mToastUtils;
    }

    private ToastUtils() {
    }

}
